/*
 * AddonTable.java
 *
 * Created on August 4, 2008, 1:54 PM
 */

package jwu2.gui.table;

import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import jwu2.core.Addon;
import jwu2.gui.AddonInfoFrame;
import jwu2.gui.GUIEventDispatcher;
import jwu2.gui.GUIEventListener;


/**
 *
 * @author  jbk
 */
public class AddonTable extends javax.swing.JPanel implements GUIEventListener {
    private String selectedName = "";
    private int currentSelection = -1;
    private tableType tableViewType;
    public enum tableType {
        ADD, REMOVE, ADD_BUNDLE, REMOVE_BUNDLE
    }
    
    /** Creates new form AddonTable */
    public AddonTable(tableType tableViewType) {
        initComponents();
        this.tableViewType = tableViewType;
        GUIEventDispatcher.addListener(this);
        
        DescriptionCellRenderer descriptionColumn = new DescriptionCellRenderer();
        InstallCellEditor nameColumn = new InstallCellEditor(tableViewType);
        addonTable.getColumnModel().getColumn(2).setCellRenderer(descriptionColumn);
        //addonTable.getColumnModel().getColumn(1).setCellRenderer(nameColumn);
        addonTable.getColumnModel().getColumn(1).setCellEditor(nameColumn);
        
        addonTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        
        addonTable.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN);
        addonTable.getColumnModel().getColumn(0).setMaxWidth(10);
        addonTable.getColumnModel().getColumn(1).setMaxWidth(200);
        
        addonTable.setValueAt(new Addon("Testaddon 1"),0,1);
        addonTable.setValueAt(new Addon("Anotherdon"),1,1);
    }
    
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        addonTable = new javax.swing.JTable();

        addonTable.setAutoCreateRowSorter(true);
        addonTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {"+", "", "<html>Because of the open nature of Wikipedia (unlike EOS, anyone can edit most of Wikipedia anonymously), it comes off a bit like a cross between USENET and IRC. People will anonymously edit your prized, painstakingly-created article with PWNT JOO DICKFACE, as they frequently do on USENET, but you can subsequently ban them, like on IRC.<br />Install</html>"},
                {"-", null, "<html>This has the effect of minimizing such vandalism and has allowed Wikipedia to grow to behemoth size.</html>"},
                {"+", null, null},
                {null, null, null}
            },
            new String [] {
                "", "Addon", "Info"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.Object.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, true, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        addonTable.setAlignmentY(0.0F);
        addonTable.setIntercellSpacing(new java.awt.Dimension(0, 1));
        addonTable.setMinimumSize(new java.awt.Dimension(400, 400));
        addonTable.setSelectionForeground(new java.awt.Color(184, 207, 229));
        addonTable.setShowVerticalLines(false);
        addonTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                addonTableMousePressed(evt);
            }
        });
        jScrollPane1.setViewportView(addonTable);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(0, 12, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 375, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(0, 13, Short.MAX_VALUE)))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(0, 12, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 275, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(0, 13, Short.MAX_VALUE)))
        );
    }// </editor-fold>//GEN-END:initComponents

private void addonTableMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_addonTableMousePressed
    Point p = evt.getPoint();
    int row = addonTable.rowAtPoint(p);
    addonTable.editCellAt(row, 1);
}//GEN-LAST:event_addonTableMousePressed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTable addonTable;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables

    public void onInstallAddonEvent(Addon addon) {
        // If an addon is installed we remove it from the list
        if (tableViewType==tableType.ADD){
            for (int i = 0; i<addonTable.getRowCount(); i++){
                if (addonTable.getValueAt(i, 1)==addon){
                    ((DefaultTableModel)addonTable.getModel()).removeRow(i);
                    break;
                }
            }
        }
    }

    public void onRemoveAddonEvent(Addon addon) {
        // If an addon is removed we remove it from the list of removable addons
        if (tableViewType==tableType.REMOVE){
            for (int i = 0; i<addonTable.getRowCount(); i++){
                if (addonTable.getValueAt(i, 1)==addon){
                    ((DefaultTableModel)addonTable.getModel()).removeRow(i);
                    break;
                }
            }
        }
    }

    public void onBundleAddAddonEvent(Addon addon) {
    }

    public void onBundleRemoveAddonEvent(Addon addon) {
    }

}
